/*
 * @Author: Robin
 * @Date: 2021-05-21 16:21:43
 * @LastEditors: Robin
 * @LastEditTime: 2021-05-27 16:34:16
 * @Description: 解析易码excel
 */
package main

import (
	"encoding/csv"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"strings"
)

type Computer struct {
	Name       string      `json:"name"`       // 资产名称
	Img        ComputerImg `json:"img"`        // 资产图片
	Department string      `json:"department"` // 部门
	Location   string      `json:"location"`   // 办公地点
	User       string      `json:"user"`       // 使用人
	Number     string      `json:"number"`     // 设备编号
	Type       string      `json:"type"`       // 设备种类
	CPU        string      `json:"CPU"`        // CPU
	Board      string      `json:"board"`      // 主板
	SSD        string      `json:"SSD"`        // 固态硬盘
	HD         string      `json:"HD"`         // 硬盘
	Memory     string      `json:"memory"`     // 内存
	GPU        string      `json:"GPU"`        // 显卡
	// Monitor            string      `json:"monitor"`             // 显示器
	CD            string `json:"CD"`             // 光驱
	ServiceNumber string `json:"service_number"` // 服务编号
	QuickNumber   string `json:"quick_number"`   // 快速服务编码
	ComputerName  string `json:"computer_name"`  // 机器名
	IP            string `json:"ip"`             // IP地址
	MAC           string `json:"mac"`            // MAC地址
	Domain        string `json:"domain"`         // 域账户名
	BuyTime       string `json:"buy_time"`       // 购买时间
	WarrantyTime  string `json:"warranty_time"`  // 保修期限
	Supplier      string `json:"supplier"`       // 供应商
	// Remark string `json:"remark"` // 备注
	Mouse              string   `json:"mouse"`               // 鼠标
	Keyboard           string   `json:"keyboard"`            // 键盘
	CDG                string   `json:"CDG"`                 // CDG
	IPGUARD            string   `json:"IPGUARD"`             // IPGUARD
	OS                 string   `json:"os"`                  // OS（OEM）
	Classify           string   `json:"classify"`            // 分类
	OSNumber           string   `json:"os_number"`           // OS序列号
	OSSoftware         []string `json:"os_software"`         // 系统软件
	DevSoftware        []string `json:"dev_software"`        // 运维软件
	OfficeSoftware     []string `json:"office_software"`     // 办公软件
	TwodSoftware       []string `json:"twod_software"`       // 2D制图软件
	ThreedSoftware     []string `json:"threed_software"`     // 3D制图软件
	AntivirusSoftware  []string `json:"antivirus_software"`  // 杀毒软件
	FinancialSoftware  []string `json:"financial_software"`  // 财务软件
	ProgramSoftware    []string `json:"program_software"`    // 编程软件
	SimulationSoftware []string `json:"simulation_software"` // 仿真软件
	Auth               []string `json:"auth"`                // 权限
}

type ComputerImg struct {
	ID   int    `json:"id"`
	Path string `json:"path"`
}

const TOKEN = "oXtzOKykJjvh5r6IypiQ4Beb5qNluCU9"
const TPL_ID = "8"

func main() {
	// 准备读取文件
	fileName := "easyqr_excel.csv"
	fs, _ := os.Open(fileName)
	r := csv.NewReader(fs)
	content, err := r.ReadAll()
	if err != nil {
		log.Fatalln(err)
	}
	defer fs.Close()
	// data := ""
	for _, row := range content {
		// 创建json
		var c Computer
		c.Department = row[1]
		c.Location = row[2]
		c.User = row[3]
		c.Number = row[4]
		c.Name = row[4]
		c.Type = row[5]
		c.CPU = row[6]
		c.Board = row[7]
		c.SSD = row[8]
		c.HD = row[9]
		c.Memory = row[10]
		c.GPU = row[11]
		// c.Monitor = row[12]
		c.CD = row[13]
		c.ServiceNumber = row[14]
		c.QuickNumber = row[15]
		c.ComputerName = row[16]
		c.IP = row[17]
		c.MAC = row[18]
		c.Domain = row[19]
		c.BuyTime = row[20]
		c.WarrantyTime = row[21]
		c.Supplier = row[22]
		// c.Remark = row[23]
		c.Mouse = row[24]
		c.Keyboard = row[25]
		c.CDG = row[26]
		c.IPGUARD = row[27]
		c.OS = row[28]
		c.Classify = row[29]
		c.OSNumber = row[30]
		c.OSSoftware = make([]string, 0)
		if row[31] != "" {
			c.OSSoftware = append(c.OSSoftware, "winxp"+row[31])
		}
		if row[32] != "" {
			c.OSSoftware = append(c.OSSoftware, "win7"+row[32])
		}
		if row[33] != "" {
			c.OSSoftware = append(c.OSSoftware, "win8/win10"+row[33])
		}
		if row[34] != "" {
			c.OSSoftware = append(c.OSSoftware, "apple"+row[34])
		}
		if row[35] != "" {
			c.OSSoftware = append(c.OSSoftware, "winsvr2003"+row[35])
		}
		if row[36] != "" {
			c.OSSoftware = append(c.OSSoftware, "winsvr2008"+row[36])
		}
		c.DevSoftware = make([]string, 0)
		if row[37] != "" {
			c.DevSoftware = append(c.DevSoftware, "citrix"+row[37])
		}
		if row[38] != "" {
			c.DevSoftware = append(c.DevSoftware, "SQL2008"+row[38])
		}
		if row[39] != "" {
			c.DevSoftware = append(c.DevSoftware, "Seav-u"+row[39])
		}
		c.OfficeSoftware = make([]string, 0)
		if row[40] != "" {
			c.OfficeSoftware = append(c.OfficeSoftware, "office2007/2010/2013"+row[40])
		}
		if row[41] != "" {
			c.OfficeSoftware = append(c.OfficeSoftware, "WPS"+row[41])
		}
		if row[42] != "" {
			c.OfficeSoftware = append(c.OfficeSoftware, "FOXMAIL"+row[42])
		}
		if row[43] != "" {
			c.OfficeSoftware = append(c.OfficeSoftware, "压缩软件"+row[43])
		}
		if row[44] != "" {
			c.OfficeSoftware = append(c.OfficeSoftware, "adobe reader 7.0"+row[44])
		}
		c.TwodSoftware = make([]string, 0)
		if row[45] != "" {
			c.TwodSoftware = append(c.TwodSoftware, "auto acd2012/2013"+row[45])
		}
		if row[46] != "" {
			c.TwodSoftware = append(c.TwodSoftware, "中望cad2015"+row[46])
		}
		if row[47] != "" {
			c.TwodSoftware = append(c.TwodSoftware, "Mazak camware"+row[47])
		}
		if row[48] != "" {
			c.TwodSoftware = append(c.TwodSoftware, "MasterCAM9.1"+row[48])
		}
		if row[49] != "" {
			c.TwodSoftware = append(c.TwodSoftware, "photoshop"+row[49])
		}
		if row[50] != "" {
			c.TwodSoftware = append(c.TwodSoftware, "Adobe Illustrator"+row[50])
		}
		c.ThreedSoftware = make([]string, 0)
		if row[51] != "" {
			c.ThreedSoftware = append(c.ThreedSoftware, "Solidwork2012/2013/2014"+row[51])
		}
		if row[52] != "" {
			c.ThreedSoftware = append(c.ThreedSoftware, "ug7.0"+row[52])
		}
		if row[53] != "" {
			c.ThreedSoftware = append(c.ThreedSoftware, "ESPRIT"+row[53])
		}
		if row[54] != "" {
			c.ThreedSoftware = append(c.ThreedSoftware, "MATRIX_CAM(5轴）"+row[54])
		}
		if row[55] != "" {
			c.ThreedSoftware = append(c.ThreedSoftware, "Solidworks2013 CAM"+row[55])
		}
		if row[56] != "" {
			c.ThreedSoftware = append(c.ThreedSoftware, "Modu Flow2012/2013"+row[56])
		}
		c.AntivirusSoftware = make([]string, 0)
		if row[57] != "" {
			c.AntivirusSoftware = append(c.AntivirusSoftware, "OfficeScan"+row[57])
		}
		c.FinancialSoftware = make([]string, 0)
		if row[58] != "" {
			c.FinancialSoftware = append(c.FinancialSoftware, "用友软件网络版"+row[58])
		}
		if row[59] != "" {
			c.FinancialSoftware = append(c.FinancialSoftware, "开票软件"+row[59])
		}
		c.ProgramSoftware = make([]string, 0)
		if row[60] != "" {
			c.ProgramSoftware = append(c.ProgramSoftware, "Powerbuilder"+row[60])
		}
		if row[61] != "" {
			c.ProgramSoftware = append(c.ProgramSoftware, "Visual_Studio"+row[61])
		}
		c.SimulationSoftware = make([]string, 0)
		if row[62] != "" {
			c.SimulationSoftware = append(c.SimulationSoftware, "ANSYS 14.5"+row[62])
		}
		c.Auth = make([]string, 0)
		if row[63] == "启用" {
			c.Auth = append(c.Auth, "USB权限")
		}
		if row[64] == "启用" {
			c.Auth = append(c.Auth, "微信")
		}
		if row[65] == "启用" {
			c.Auth = append(c.Auth, "EMAIL")
		}
		if row[66] == "启用" {
			c.Auth = append(c.Auth, "WEB")
		}
		jsonBytes, err := json.Marshal(&c)
		if err != nil {
			fmt.Println(err)
			continue
		}
		fmt.Println(string(jsonBytes))
		req, _ := http.NewRequest("POST", "https://y23y.cn:8000/qrcode/create", strings.NewReader("tpl_id="+TPL_ID+"&name="+c.Name+"&attrs="+string(jsonBytes)+"&res="))
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
		req.Header.Set("Authorization", TOKEN)
		resp, err := http.DefaultClient.Do(req)
		// resp, err := http.Post("https://y23y.cn:8000/qrcode/create", "application/x-www-form-urlencoded", strings.NewReader("tpl_id="+TPL_ID+"&name="+c.Name+"&attrs="+string(jsonBytes)+"&res="))
		if err != nil {
			fmt.Println(err)
			continue
		}
		body, _ := ioutil.ReadAll(resp.Body)
		fmt.Println(string(body))
		resp.Body.Close()
	}
	// ioutil.WriteFile("json.json", []byte(data), 0666)
}
